#######################################################################
## author:    Michael L. Wicki
## contact:   wimi@ethz.ch, ETH Zurich
## file name: conjoint_EVZH.R
## Context:   ISTP mobility lab - EVZH
## started:   2021-03-12
## Summary:   Analysis Survey Conjoint EVZH
#######################################################################

rm(list = ls())
getwd()
setwd("Z:/02_People/Wicki Michael/EVZH")
set.seed(42)

library(cjoint)
library(Matrix)
library(ggthemes)
library(ggplot2)
library(data.table)
library(dplyr)
library(nFactors)
library(lme4)
library(effects)
library(ggthemes)
library(tidyr)
library(forcats)
library(texreg)
library(influence.ME)



########### load conjoint data ###############
load("Z:/02_People/Wicki Michael/EVZH/EV-ZH_Manipulation/newdata/evzh_sociodemographics.RData")

##### Q45 #####
df = data.table(evzh) 
df_q45 <- df[,c("Q45_allrad", "Q45_andere", "Q45_auswahl", "Q45_belegt", "Q45_beschl", "Q45_ferien", "Q45_geschw", "Q45_laden_arb", "Q45_laden_haus",
                "Q45_laden_uwegs", "Q45_leiser", "Q45_motor", "Q45_preis", "Q45_reichw", "Q45_tanken", "Q45_umwelt", "Q45_unterh", "Q45_wert", "Q45_zuver_auto", "Q45_zuver_laden"), with=FALSE] %>% 
  gather(Q45_allrad:Q45_zuver_laden, key = "Q", value = "V")

df_q45$count <- ifelse(df_q45$V==1, df_q45$Q, NA)
df_q45 <- subset(df_q45, !is.na(count))

df_q45 <- within(df_q45, 
                 count <- factor(count, 
                                      levels=names(sort(table(count), 
                                                        decreasing=TRUE))))

df_q45$count <- ifelse(df_q45$Q=="Q45_allrad", "No four-wheel drive",
                   ifelse(df_q45$Q=="Q45_andere", "Other reasons",
                          ifelse(df_q45$Q=="Q45_auswahl", "Too little choice of models",
                                 ifelse(df_q45$Q=="Q45_belegt", "Occupied charging stations",
                                        ifelse(df_q45$Q=="Q45_beschl", "Slower acceleration",
                                               ifelse(df_q45$Q=="Q45_ferien", "Not suitable for holidays",
                                                      ifelse(df_q45$Q=="Q45_geschw", "Maximum speed too low", 
                                                             ifelse(df_q45$Q=="Q45_laden_arb", "Lack of charging possibility at work", 
                                                                    ifelse(df_q45$Q=="Q45_laden_haus", "Lack of charging possibility at home", 
                                                                           ifelse(df_q45$Q=="Q45_laden_uwegs", "Lack of public charging",
                                                                                  ifelse(df_q45$Q=="Q45_leiser", "More dangerous, because quieter",
                                                                                         ifelse(df_q45$Q=="Q45_motor", "Lower engine power",
                                                                                                ifelse(df_q45$Q=="Q45_preis", "Purchase price too high",
                                                                                                       ifelse(df_q45$Q=="Q45_reichw", "Range too small",
                                                                                                              ifelse(df_q45$Q=="Q45_tanken", "Charging duration too long",
                                                                                                                     ifelse(df_q45$Q=="Q45_umwelt", "Unclear whether more environmentally friendly", 
                                                                                                                            ifelse(df_q45$Q=="Q45_unterh", "Maintenance costs too high", 
                                                                                                                                   ifelse(df_q45$Q=="Q45_wert", "Risk of value loss",
                                                                                                                                          ifelse(df_q45$Q=="Q45_zuver_auto", "Lower reliability",
                                                                                                                                                 ifelse(df_q45$Q=="Q45_zuver_laden", "Unreliable charging infrastructure", NA))))))))))))))))))))
df_q45 <- within(df_q45, 
                 count <- factor(count, 
                                 levels=names(sort(table(count), 
                                                   decreasing=FALSE))))

graphQ45 <- ggplot(df_q45, aes(x=count)) + 
  geom_bar(aes(y = (..count..)*100/7342*5), colour="black", fill="grey") + 
  geom_text(stat = "count", aes(label = sprintf("%.01f%%", round((..count..)*100/7342*5, 1)), y = (..count..)*100/7342*5), hjust=-0.15, fontface = "bold") +
  theme_bw()+ 
  labs(x="", y="Share of selected answers in per cent") +
  coord_flip() +
  labs(title = "") +
  ylim(0, 65) +
  theme(panel.grid.major.x = element_blank(),
        panel.grid.minor.y = element_blank(),
        panel.grid.major.y = element_line(size = 0.6, linetype = 'solid', colour = "grey80"),
        panel.border = element_blank(),
        strip.background.x = element_blank(),
        text = element_text(size=20))

graphQ45

ggsave(graphQ45, filename = "graphObstacles.png", width = 12, height = 10, unit="in", dpi = 600)


